package com.caochenlei.singleton.mode6;

public class SingletonTest {
    public static void main(String[] args) {
        Singleton singleton1 = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();
        System.out.println(singleton1 == singleton2);
    }
}

//登记式(静态内部类)
class Singleton {
    //私有构造方法，防止外部new
    private Singleton() {}

    //提供一个私有的静态内部类
    private static class SingletonHolder {
        private final static Singleton INSTANCE = new Singleton();
    }

    //向外部提供一个公开获取方法
    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }
}
